# Generated migration for loading initial data
from django.db import migrations
from django.core.management import call_command
import os


def load_initial_data(apps, schema_editor):
    """加载初始数据"""
    # 获取当前文件的目录
    current_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    fixture_path = os.path.join(current_dir, 'initial_data.json')

    if os.path.exists(fixture_path):
        call_command('loaddata', fixture_path, verbosity=2)
        print("✅ 初始数据加载完成")
    else:
        print("⚠️ 初始数据文件不存在，跳过数据加载")


def reverse_load_initial_data(apps, schema_editor):
    """回滚操作 - 不执行任何操作"""
    print("⚠️ 数据回滚操作需要手动处理")


class Migration(migrations.Migration):

    dependencies = [
        ('cmdb', '0024_add_dynamic_menu_fields'),  # 依赖最新的migration
        ('ansible_management', '0003_auto_20250621_2023'),  # 确保ansible模块也已迁移
    ]

    operations = [
        migrations.RunPython(load_initial_data, reverse_load_initial_data),
    ]
